<!--
 * @Author: yaojian66 1327629137@qq.com
 * @Date: 2022-06-28 15:48:40
 * @LastEditors: yaojian66 1327629137@qq.com
 * @LastEditTime: 2022-06-28 16:53:49
 * @FilePath: \geyao\第八十二题 数组排序.html
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数组排序</title>
</head>
<body>
  <button class='up' onclick="changeUp()">销量升序</button>
  <button class='down' onclick="changeDown()">销量降序</button>
  <ul></ul>
<!-- 请补全JavaScript代码，根据预设代码中的数组，实现以下功能：
1. 列表只展示数组中的name属性
2. 实现点击"销量升序"按钮，列表内容按照销量升序重新渲染
3. 实现点击"销量降序"按钮，列表内容按照销量降序重新渲染
注意：
1. 必须使用DOM0级标准事件（onclick） -->
  <script>
    
      var cups = [
          { type: 1, price: 100, color: 'black', sales: 3000, name: '牛客logo马克杯' },
          { type: 2, price: 40, color: 'blue', sales: 1000, name: '无盖星空杯' },
          { type: 4, price: 60, color: 'green', sales: 200, name: '老式茶杯' },
          { type: 3, price: 50, color: 'green', sales: 600, name: '欧式印花杯' }
      ]
      var ul = document.querySelector('ul');
      var upbtn = document.querySelector('.up');
      var downbtn = document.querySelector('.down');
      // 补全代码
      upbtn.onclick = function(){
                ul.innerHTML = ''
                cups.sort(function(a,b){
                    return a.sales - b.sales;
                })
               
                for(let i =0;i <cups.length;i++){
                    let li = document.createElement('li');
                    li.innerHTML  = `${cups[i].name}`
                    ul.appendChild(li)
                }
            }
            downbtn.onclick = function(){
                ul.innerHTML = '';
                cups.sort(function(a,b){
                    return b.sales - a.sales;
                })
               
                for(let i =0;i <cups.length;i++){
                    let li = document.createElement('li');
                    li.innerHTML  = `${cups[i].name}`
                    ul.appendChild(li)
                }
            }
      
  </script>
</body>
</html>